package com.xinlianshiye.im.dao.impl;

import com.xinlianshiye.im.dao.BaseRepository;
import com.xinlianshiye.im.dao.custom.FriendApplicationDaoCustom;
import com.xinlianshiye.im.bean.FriendApplication;
import com.xinlianshiye.im.bean.QFriendApplication;

import java.util.List;

public class FriendApplicationDaoImpl extends BaseRepository implements FriendApplicationDaoCustom {

    @Override
    public List<FriendApplication> findAllByUserId(Integer userId) {
        QFriendApplication friendApplication = QFriendApplication.friendApplication;
        return queryFactory
                .select(friendApplication)
                .from(friendApplication)
                .where(friendApplication.userId.eq(userId).or(friendApplication.targetUserId.eq(userId)))
                .orderBy(friendApplication.createTime.desc())
                .fetch();
    }

    @Override
    public Integer countApplyMsg(Integer id) {
        QFriendApplication friendApplication = QFriendApplication.friendApplication;
        Integer before = (int) queryFactory.selectFrom(friendApplication).where(friendApplication.userId.eq(id)).where(friendApplication.haveRead.eq(false)).fetchCount();
        Integer after = (int) queryFactory.selectFrom(friendApplication).where(friendApplication.targetUserId.eq(id)).where(friendApplication.targetHaveRead.eq(false)).fetchCount();
        return before + after;
    }
}
